import store from "@/store/index.js";
export default {
	common: {
		bastUrl: "http://yun88.top:4001/api",
		data: {},
		header: {
			"Content-Type": "application/json",
			"Content-Type": "application/x-www-form-urlencoded"
		},
		method: "GET",
		dataType: "json"
	},
	request(options) {
		uni.showLoading({
			title: "加载中..."
		});
		options.url = this.common.bastUrl + options.url;
		options.data = options.data || this.common.data;
		options.header = options.header || this.common.header;
		options.method = options.method || this.common.method;
		options.dataType = options.dataType || this.common.dataType;
		//需要验证token
		if(options.header.token){
			options.header.token = store.state.user.token
			//如果当前没有token
			if(!options.header.token){
				uni.showToast({
					title:"请先登录",
					icon:"none"
				})
				uni.navigateTo({
					url:"/pages/login/login"
				})
			}
		}
		return new Promise((res, rej) => {
			uni.request({
				...options,
				success: (result) => {
					if (result.statusCode != 200) {
						return rej();
					}
					uni.hideLoading();
					//console.log(result);
					res(result);
				}
			})
		})
	}
}
